package servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.Blog;
import model.BlogDao;
import model.User;
import model.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ASUS
 * Date: 2024-01-17
 * Time: 11:45
 */
@WebServlet("/getAuthorInfo")
public class AuthorInfoServlet extends HttpServlet {
    private ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.先拿到请求中的blogId
        String blogId = req.getParameter("blogId");
        if(blogId == null){
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("请求中没有对应的blogId");
            return;
        }

        //2.在blog表中查询到对应的Blog对象
        BlogDao blogDao = new BlogDao();
        Blog blog = blogDao.getBlog((Integer.parseInt(blogId)));
        if(blog == null){
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("blogId没找到！");
            return;
        }
        //3.根据blog对象中的 userId，根据user表中查到对应的作者
        UserDao userDao = new UserDao();
        User user = userDao.getUserById(blog.getUserId());
        if(user == null){
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("userId没有找到！");
            return;
        }
        //4.将user对象返回给浏览器
        user.setPassword("");
        String respJson = objectMapper.writeValueAsString(user);
        resp.setContentType("application/json;charset=utf8");
        resp.getWriter().write(respJson);


    }
}
